package org.dfl.plugin.editor.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.dfl.core.model.XMLLoaderException;
import org.dfl.core.model.transitions.JarTransition;
import org.dfl.core.model.transitions.Transition;
import org.dfl.core.model.transitions.TransitionFactory;
import org.dfl.messages.Messages;
import org.dfl.plugin.ProjectConstraints;
import org.dfl.plugin.editor.figures.FiguresConstants;
import org.dfl.plugin.editor.markers.ModelMarkersContainer;
import org.dfl.plugin.editor.markers.ModelProblem;
import org.eclipse.swt.graphics.Font;
import org.jdom.Element;

/**
 * editor's model element that represents a transition
 * @author Piotr Wlodarczyk
 *
 */
@SuppressWarnings("serial")
public class TransitionModelElement extends Shape{
	private Transition transition;
	private boolean running;

	public TransitionModelElement(Transition t) {
		setTransition(t);
		if(transition.getName()==null || transition.getName().trim().equals("")){ //$NON-NLS-1$
			transition.setName(transition.getId());
		}
	}
	
	public TransitionModelElement(Element elem, DflDiagram d) throws XMLLoaderException {
		setDiagram(d);
		load(elem);
	}
	
	
	public String getId(){
		return transition.getId();
	}

	public Transition getTransition() {
		return transition;
	}

	@Override
	public String getLabelText() {
		return getTransition().getName();
	}
	
	@Override
	public String getTipText() {
		return Messages.TransitionModelElement_tipId+getTransition().getId()+Messages.TransitionModelElement_tipName+getTransition().getName()+"\n\n"+transition.getDescription()+"\n\n"+transition.getIODescription(); //$NON-NLS-3$ //$NON-NLS-4$
	}
	
	@Override
	protected Font getLabelFont(){
		return FiguresConstants.transitionFont; 
	}
	
	@Override
	public int getMaxLabelHeight() {
		return FiguresConstants.transitionMaxHeight;
	}

	@Override
	public int getMaxLabelWidth() {
		return FiguresConstants.transitionMaxWidth;
	}
	
	public void load(Element elem) throws XMLLoaderException {
		Transition t = TransitionFactory.getEmptyTransition(elem);
		if(t instanceof JarTransition){
			try{
				String libPath = getDiagram().getProjectPath();
				if(new File(libPath + ProjectConstraints.PROJECT_DIR_LIB).exists()){ 
					libPath+=ProjectConstraints.PROJECT_DIR_LIB;
				}
				((JarTransition)t).load(elem,libPath);
			}catch(Throwable e){
				t.load(elem);	
			}
		}else{
			t.load(elem); 
		}
		setTransition(t);
		super.load(elem.getChild("graphics"));
	}
	
	public Element save() {
		Element root = transition.save();
		Element graphicsElem =  super.save();
		root.addContent(graphicsElem);
		return root;
	}

	/************************************* Running transition ***************************/
	public void runningFinished() {
		running = false;
		firePropertyChange(SELF_PROP, null, running);
	}

	public void runningStarted() {
		running = true;
		firePropertyChange(SELF_PROP, null, running);
	}

	public boolean isRunning() {
		return running;
	}
	
	@Override
	public Collection<ModelProblem> getProblems() {
		Collection<ModelProblem> problems = new ArrayList<ModelProblem>();
			
		// check inputs
		if(!getTransition().areAllInputsDefined()){
			problems.add(ModelMarkersContainer.createError(Messages.TransitionModelElement_problemNotAllInputs,Messages.TransitionModelElement_name,getTransition().getName()));
		}
			
		// check outputs
		if(getTransition().getOutputEdges().isEmpty()){
			problems.add(ModelMarkersContainer.createWarning(Messages.TransitionModelElement_problemNotAllOutputs,Messages.TransitionModelElement_name,getTransition().getName()));
		}
		
		return problems;
	}

	private void setTransition(Transition transition) {
		this.transition = transition;
		try{
			if(transition instanceof JarTransition){
				String libPath = getDiagram().getProjectPath();
				if(new File(libPath + ProjectConstraints.PROJECT_DIR_LIB).exists()){ 
					libPath+=ProjectConstraints.PROJECT_DIR_LIB;
				}
				((JarTransition)transition).setDir(libPath);
			}
		}catch(Throwable e){}
	}
	
	
}
